package zhangxinhua.lanqiaoyun.sp3.v002;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author zxhtom
 * 2022/8/8
 */
public class CutDeptAns {
    public void cut(List<DeptSp3> deptSp3List) {
        if (deptSp3List == null || deptSp3List.size() == 0) {
            return;
        }
        List<DeptSp3> parentDeleteList = new ArrayList<>();
        for (int i = 0; i < deptSp3List.size(); i++) {
            DeptSp3 dept = deptSp3List.get(i);
            int deptNo = dept.getDeptNo();
            if (isShuixianhua(deptNo) || isPrime(deptNo)) {
                //cut
                deptSp3List.remove(i);
                if (dept.getParentDept() != null) {

                    parentDeleteList.add(dept.getParentDept());
                }
            } else {
                cut(dept.getChildren());
            }
        }
        List<String> uuidList = parentDeleteList.stream().map(DeptSp3::getDeptName).collect(Collectors.toList());
        deleteSingleDeptList(deptSp3List, uuidList);
    }

    private List<DeptSp3> deleteSingleDeptList(List<DeptSp3> deptList ,List<String> uuidList) {
        if (deptList == null || deptList.size() == 0) {
            return null;
        }
        Iterator<DeptSp3> iterator = deptList.iterator();
        while (iterator.hasNext()) {
            DeptSp3 next = iterator.next();
            if (uuidList.contains(next.getDeptName())) {
                iterator.remove();
            }
            next.setChildren(deleteSingleDeptList(next.getChildren(), uuidList));
        }
        return null;
    }

    public boolean isShuixianhua(int num) {
        if (num < 100 || num >= 1000) {
            return false;
        }
        int oldNum=num;
        int result = 0;
        while (num / 10 != 0) {
            Double pow = Math.pow(num % 10, 3);
            int i = pow.intValue();
            result += i;
            num /= 10;
        }
        result += num;

        return result == oldNum;
    }

    public boolean isPrime(int num) {
        if (num <= 1 || num > 2 && num % 2 == 0) {
            return false;
        } else if (num == 2) {
            return true;
        }
        for (int i = 3; i <= Math.sqrt(num); i += 2) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        CutDeptAns ans = new CutDeptAns();
        System.out.println(ans.isShuixianhua(999));
        System.out.println(ans.isShuixianhua(153));
        System.out.println(ans.isPrime(153));
        System.out.println(ans.isPrime(7));
    }
}
